<?php
class BaseBlock{
	var $name = "";
	var $formName;
	var $template = "ctype/views/block/general.php";
	var $properties = array();
	var $typeElements = array();
	var $required = FALSE;
	
	var $ci;
	
	/*
	 * Contenido estatico asociado
	 */
	var $static = array(
		"javascript" => array(
		),
		"style" => array(
		)		
	);
	/*
	 * Campos
	 */
	var $fields;
	
	function BaseBlock(){
		$this->name = strtolower(get_class($this));
		$this->ci =& get_instance();
	}
	
	function setName($name){
		$this->name = $name;
	}
	
	function getName(){
		return $this->name;
	}
	
	function setFormName($name){
		$this->formName = $name;
	}
	
	function getFormName(){
		return $this->formName;
	}
	
	function getRequired(){
		return $this->required;
	}
	
	function setRequired($required){
		$this->required = $required;
	}
	
	function addProperty($name, $value){
		$this->properties[$name] = $value;
	}
	
	function getProperty($name){
		if(isset($this->properties[$name])){
			return $this->properties[$name];
		}
		
		return FALSE;
	}
	
	function addProperties($properties){
		if($properties != NULL){
			foreach($properties as $key=>$value){
				$this->addProperty($key, $value);
			}
		}
	}
	
	function getProperties(){
		return $this->properties;
	}
	
	function addStatic($type, $path){
		//echo "Añadiendo: $path";
		if(isset($this->static[$type])){
			//@TODO Comprobar si ya existe la libreria a añadir
			if(array_search($path, $this->static[$type]) === FALSE){
				array_push($this->static[$type], $path);
			}
		}
	}
	
	function addStatics($statics){
		//echo "<br/>Añadiendo statics";
		$keys = array_keys($statics);
		//print_r($statics);
		foreach($keys as $key){
			$staticsTmp = $statics[$key];
			foreach($staticsTmp as $static){
				$this->addStatic($key, $static);
			}
		}
	}
	
	function getStatics(){
		return $this->static;
	}
	
	function addTypeElement($typeElement){
		if($typeElement != NULL){
			if(!isset($this->typeElements[$typeElement->getName()]) || $this->typeElements[$typeElement->getName()] = NULL){
				$this->typeElements[$typeElement->getName()] = $typeElement;
			} else{
				echo "El campo " . $typeElement->getName() . " se encuentra duplicado";
			}
		}
	}
	
	function getTypeElement($name){
		if(isset($this->typeElements[$name])){
			return $this->typeElements[$name];
		}
	
		return FALSE;
	}
	
	public function view($node = NULL, $data = NULL){
		$fields = "";
		foreach($this->typeElements as $typeElement){
			
			$this->addStatics($typeElement->getStatics());
			
			if($node != NULL && $node !== FALSE){
				$dataTypeElement = NULL;
				if(isset($data[$typeElement->getName()])){
					$dataTypeElement = $data[$typeElement->getName()];
				}
				$fields .= $typeElement->view($node, $dataTypeElement);
			} else{
				$fields .= $typeElement->view();
			}
			
		}
		
		$name = $this->formName;
		$properties = $this->properties;
		$static = $this->static;
			
		ob_start();
		eval('?>'.preg_replace("/;*\s*\?>/", "; ?>", str_replace('<?=', '<?php echo ', file_get_contents($this->template, TRUE))));
		$view = ob_get_contents();
		@ob_end_clean();
		
		return $view;
	}
	
	function checkCorrectSave(){
		
		//@TODO	
		$correct = FALSE;
		
		if($this->ci->form_validation->run()){	
			$correct = TRUE;
		}
		foreach($this->typeElements as $typeElement){
			$correct = $correct & $typeElement->checkCorrectSave();
		}
		
		return $correct;
	}
	
	function save($node=NULL, $data=NULL){
		//Comprobar si tiene repeat para que sea un array..
		
		$dataBlock = array();
		
		foreach($this->typeElements as $typeElement){
			$value = NULL;
			
			if($node != NULL && $node !== FALSE){
				$dataTypeElement = NULL;
				if(isset($data[$typeElement->getName()])){
					$dataTypeElement = $data[$typeElement->getName()];
				}
				$value = $typeElement->save($node, $dataTypeElement);
			} else{
				$value = $typeElement->save();
			}
			
			if($value != NULL){
				$dataBlock[$typeElement->getName()] = $value;
			}
			
		}
		
		//@TODO si $dataBlock es vacio, no devolver nada?
		//$data[$this->name] = $dataBlock;
		
		//return $data;
		
		return $dataBlock;
	}
	
	function delete($node=NULL, $data=NULL){
		//Comprobar si tiene repeat para que sea un array..
		
		$dataBlock = array();
		
		foreach($this->typeElements as $typeElement){
			
			if($node != NULL && $node !== FALSE){
				$dataTypeElement = NULL;
				if(isset($data[$typeElement->getName()])){
					$dataTypeElement = $data[$typeElement->getName()];
				}
				$correctDelete = $typeElement->delete($node, $dataTypeElement);
			} else{
				//no correcto
			}
			
			if($value != NULL){
				$dataBlock[$typeElement->getName()] = $value;
			}
			
		}
		
		//@TODO si $dataBlock es vacio, no devolver nada?
		//$data[$this->name] = $dataBlock;
		
		//return $data;
		
		return $dataBlock;
	}
}
?>